<template>
  <div>
    <!-- <div class="text-red fz20">通过地址栏获取报表id，再获取msgidlist，换取msg</div> -->
    <!-- <pre>
      {{ rfInfo }}
    </pre> -->
    <el-divider content-position="center">
      <i class="fa fa-fw fa-files-o"></i>{{ rfInfo.title }} （报表id:{{
        rfInfo.report_Id
      }}）
    </el-divider>
    <el-divider content-position="center">
      <i class="fa fa-fw fa-thumb-tack"></i>创建时间:{{ rfInfo.addTime }}
      <i class="fa fa-fw fa-pencil"></i>修改时间:{{ rfInfo.editTime }}
    </el-divider>
    <div class="">
      <!-- <pre v-if="isDev">{{ DevMsgList }}</pre> -->
      <WxMsg :msgList="msgList"></WxMsg>
    </div>
    <!-- <div class="align-cm mt20 fc-gray80" v-if="[0].includes(rfInfo.checkState)">
      未提交
    </div>
    <div class="align-cm mt20" v-if="[1].includes(rfInfo.checkState)">
      <el-button type="success" @click="审核(2)">审核通过</el-button>
      <el-button type="danger" @click="审核(3)">审核拒绝</el-button>
    </div>
    <div class="align-cm mt20 text-red" v-if="[2,3].includes(rfInfo.checkState)">
      已操作
    </div> -->
    <!-- 以上因业务流程修改而取消 -->
  </div>
</template>

<script>
import { bus } from '@/main.js';
import { mapState, mapMutations } from 'vuex';
import aboutChat from '@/mixin/aboutChat.vue';

export default {
  name: 'examine',
  mixins: [aboutChat],
  components: {
    WxMsg: () => {
      return import('@/components/wx_messageArea.vue');
    },
  },
  data() {
    return {
      isDev: process.env.NODE_ENV == 'development',
      rfInfo: {},
      msgList: [],
      DevMsgList: [],
    };
  },

  computed: {
    ...mapState(['userInfo']),
  },
  methods: {
    init: function() {
      this.ajaxData();
    },
    ajaxInfo: function() {},
    ajaxData: function() {
      let ceid = this._.get(this.$route.query, 'companyEmployeeId');
      let rfid = this._.get(this.$route.query, 'rfid');
      this.$loading();

      Promise.all([
        this.$api.报表.获取报表Info({ reportId: rfid }),
        this.$api.报表.获取报表List({ reportId: rfid }),
      ])
        .then(res => {
          if (res[0].code == 200) {
            let t = res[0].data;
            this.$set(this.rfInfo, 'report_Id', t.report_Id);
            this.$set(
              this.rfInfo,
              'company_Employee_Id',
              t.company_Employee_Id,
            );
            this.$set(this.rfInfo, 'eS_Company_Id', t.eS_Company_Id);
            this.$set(this.rfInfo, 'external_userid', t.external_userid);
            this.$set(this.rfInfo, 'title', t.title);
            this.$set(this.rfInfo, 'checkState', t.checkState);
            this.$set(this.rfInfo, 'addTime', t.addTime);
            this.$set(this.rfInfo, 'editTime', t.editTime);
          } else {
            return Promise.reject({ message: '未返回有效信息' });
          }
          if (res[1].code == 200 && res[1].data.length > 0) {
            let arr = res[1].data.map(v => {
              return v.msgid;
            });
            this.DevMsgList = arr;
            let Parr = this._.chunk(arr, 99);
            return Promise.all(
              Parr.map(msgIdsList => {
                return this.$api.报表.getList报表数据ByMsgId(
                  {},
                  {
                    wxId: res[0].data.eS_Company_Id,
                    msgIdsList: msgIdsList,
                  },
                );
              }),
            );
          }
        })
        .then(res => {
          if (
            res.every(v => {
              return v.code == 8200;
            })
          ) {
            let arr = res.reduce((t, v, i, a) => {
              return t.concat(v.result);
            }, []);
            return Promise.resolve(arr);
          }
        })
        .then(res => {
          this.msgList.splice(0, this.msgList.length);
          res.forEach(item => {
            this.msgList.push(this.format(item));
          });
        })
        .then(res => {
          // this.sortByMySelf();
          // 2024-11-29 前端再排序，避免总部后端脑残
          // 2024-12-18 取消再排序
        })
        .catch(err => {
          console.error(err);
        })
        .finally(_ => {
          this.$loading().close();
        });
      return;
    },

    sortByMySelf: function() {
      const ids = this.DevMsgList;
      let tar = this.msgList;
      // 创建一个映射，以便快速查找对象的索引
      const idIndexMap = new Map(ids.map((id, index) => [id, index]));
      // 使用sort方法和映射来排序对象数组
      tar.sort((a, b) => idIndexMap.get(a.消息id) - idIndexMap.get(b.消息id));
    },

    // 审核:function(state){
    //   let arg = {
    //     "report_Id": this._.get(this.rfInfo,'report_Id',''),
    //     "company_Employee_Id": this._.get(this.rfInfo,'company_Employee_Id',''),
    //     "eS_Company_Id":this._.get(this.rfInfo,'eS_Company_Id',''),
    //     "external_userid":this._.get(this.rfInfo,'external_userid',''),
    //     "title":this._.get(this.rfInfo,'title',''),
    //     "checkState": state,
    //   }
    //   this.$api.报表.update报表ById({},arg).then(res=>{
    //     this.$message.success('审核状态已修改')
    //     this.$set(this.rfInfo,'checkState',state)

    //   }).catch(err=>{
    //     console.error(err)
    //     this.$message.error('审核状态未修改')
    //   }).finally(_=>{})
    // },以上因业务流程修改而取消
  },
  mounted: function() {
    this.ajaxData();
  },
  updated: function() {},
};
</script>

<style></style>
